﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class LoaiKhachDAO:AbstractDAO
    {
        public static List<LoaiKhachDTO> layDSLoaiKhach()
        {
            OleDbConnection ketNoi = null;
            List<LoaiKhachDTO> dsLoaiKhach = new List<LoaiKhachDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaLoaiKhach,TenLoaiKhach FROM LoaiKhach ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while (boDoc.Read())
                {
                    LoaiKhachDTO loaiKhach = new LoaiKhachDTO();
                    loaiKhach.MaLoaiKhach = boDoc.GetInt32(0);
                    if (!boDoc.IsDBNull(1))
                        loaiKhach.TenLoaiKhach = boDoc.GetString(1);

                    dsLoaiKhach.Add(loaiKhach);
                }

            }
            catch
            {
                dsLoaiKhach = new List<LoaiKhachDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsLoaiKhach;
        }
        public static bool them(LoaiKhachDTO loaiKhach)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO LoaiKhach(TenLoaiKhach)";
                chuoiLenh += " VALUES (@TenLoaiKhach)";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@TenLoaiKhach", OleDbType.VarChar);
                thamSo.Value = loaiKhach.TenLoaiKhach;
                lenh.Parameters.Add(thamSo);


                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
    }
}
